﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Algorithm.Midlle_1706_FindBall
{
    class MainProject
    {
        static void Main(string[] args)
        {

            int[][] array = new int[][]
            {
               new int[]{ 1,1,1,1,1,1 },
                new int[]{-1,-1,-1,-1,-1,-1 },
                new int[]{1,1,1,1,1,1},
                new int[]{-1,-1,-1,-1,-1,-1 },
                //new int[]{-1,-1,-1,-1,-1 },
            };
            foreach (var item in FindBall(array))
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }
        public static int[] FindBall(int[][] grid)
        {
            //思路：根据总结的规律，进行模拟


            int[] array = new int[grid[0].Length];
            for (int i = 0; i < array.Length; i++)
            {
                int x = 0;int y = i;int index = 0;
                Console.WriteLine("----------------");
                while (true)
                {
                    if (x>= grid.Length)
                    {
                        array[i] = y;
                        break;
                    }
                    if (y<0 || y>= grid[x].Length)
                    {
                        array[i] = -1;
                        break;
                    }
                    Console.WriteLine("x : {0}; y: {1}",x,y);
                    if (grid[x][y] == -1)
                    {
                        if (index % 2 == 0)
                        {
                            if (y - 1 < 0||grid[x][y - 1] == 1)
                            {
                                array[i] = -1;
                                break;
                            }
                            y--;
                        }
                        else
                        {
                            if (y + 1 >= grid[x].Length || grid[x][y + 1] == 1)
                            {
                                array[i] = -1;
                                break;
                            }
                            x++;
                        }
                        index++;
                    }
                    else//==1
                    {
                        if (index % 2 == 0)
                        {
                         
                            if (y + 1>= grid [x].Length || grid[x][y + 1] == -1)
                            {
                                array[i] = -1;
                                break;
                            }
                            y++;
                        }
                        else
                        {
                            if (y - 1 < 0 || grid[x][y - 1] == -1)
                            {
                                array[i] = -1;
                                break;
                            }
                            x++;
                        }
                        index++;
                    }       
                }
            }
            return array;
        }
    }
}
